package com.efounder.activity;

import android.os.Bundle;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.efounder.adapter.CartOrderPagerAdapter;
import com.efounder.bean.CartEditEvent;
import com.efounder.bean.CartEditVisibleEvent;
import com.efounder.bean.CartNumberEvent;
import com.efounder.chat.activity.BaseActivity;
import com.efounder.mobilemall.R;
import com.efounder.utils.ResStringUtil;
import com.marlonmafra.android.widget.SegmentedTab;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 商城购物车和订单
 * tab
 * @author will
 */
public class MallCartOrderActivity extends BaseActivity {

    private SegmentedTab segmentControl;
    private ViewPager viewPager;
    private TextView tvEdit;
    private String[] titlesArray;
    private List<String> titles;
    private boolean cartEditVisible;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mall_activity_cart_order);
        titlesArray = getResources().getStringArray(R.array.cart_order_array);
        segmentControl = (SegmentedTab) findViewById(R.id.segment_control);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tvEdit = (TextView) findViewById(R.id.tv_edit);

        EventBus.getDefault().register(this);
        initEditListener();
        initViewPager();
        initTabLayout();
    }

    private void initEditListener() {
        tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tvEdit.getText().toString().equals(ResStringUtil.getString(R.string.mail_complete))) {
                    tvEdit.setText(R.string.mail_edit);
                    EventBus.getDefault().post(new CartEditEvent(true));
                } else {
                    tvEdit.setText(R.string.mail_complete);
                    EventBus.getDefault().post(new CartEditEvent(false));
                }
            }
        });
    }

    private void initViewPager() {
        viewPager.setAdapter(new CartOrderPagerAdapter(getSupportFragmentManager(), titlesArray));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    tvEdit.setVisibility(View.GONE);
                } else {
                    if (cartEditVisible) {
                        tvEdit.setVisibility(View.VISIBLE);
                    } else {
                        tvEdit.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initTabLayout() {
        titles = new ArrayList<>();
        for (int i = 0; i < titlesArray.length; i++) {
            titles.add(titlesArray[i]);
        }
        this.segmentControl.setupWithViewPager(this.viewPager);
        this.segmentControl.setup(titles);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCartEditVisibilityChangedEvent(CartEditVisibleEvent editVisibleEvent) {
        cartEditVisible = editVisibleEvent.isVisible();
        if (viewPager.getCurrentItem() ==0) {
            if (cartEditVisible) {
                tvEdit.setVisibility(View.VISIBLE);
            } else {
                tvEdit.setVisibility(View.GONE);
            }

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCartNumberChangeEvent(CartNumberEvent event) {
        int number = event.getNumber();
        // FIXME: 18-12-14 改变tab文字
        if (number > 0) {
            titles.remove(0);
            titles.add(0, ResStringUtil.getString(R.string.mail_frame_shop_car) + " (" + number + ")");
            segmentControl.setup(titles);
        } else {
            titles.remove(0);
            titles.add(0, ResStringUtil.getString(R.string.mail_frame_shop_car));
            segmentControl.setup(titles);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
